    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

import ch.zarzu.champions.builder.App;
import ch.zarzu.champions.builder.SystemLink;
import ch.zarzu.util.*;

public class TabRider extends JLayeredPane {
    JLabel label, left_button, mid_button, right_button;
    String app_path;
    int name_width;
    SpringLayout layout;
    boolean is_active;
    
    public TabRider(String n) {
        setName(n);
        
        layout = new SpringLayout();
        setLayout(layout);
        
        app_path = AppPath.get();
        draw();
        
        is_active = false;
    }
    
    private void draw() {
        ImageIcon icon, icon_tmp;
        
        String name = SystemLink.getInstance().translate(getName());
        
        Font font = new Font("Comic Sans MS", Font.BOLD, 11);
        FontMetrics metrics = new FontMetrics(font) {};
        Rectangle2D bounds = metrics.getStringBounds(name, null);
        name_width = (int) bounds.getWidth();
        
        label = new JLabel(name);
        label.setFont(font);
        label.setForeground(App.VERY_LIGHT_BLUE);
        add(label);
        
        icon = new ImageIcon(app_path + "images/tabs/left_tab_a.png");
        left_button = new JLabel(icon);
        add(left_button);
        
        icon = new ImageIcon(app_path + "images/tabs/right_tab_a.png");
        right_button = new JLabel(icon);
        add(right_button);
        
        
        icon_tmp = new ImageIcon(app_path + "images/tabs/mid_tab_a.png");
        icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(name_width + 4, icon_tmp.getIconHeight(), Image.SCALE_SMOOTH));
        mid_button = new JLabel(icon);
        add(mid_button);
        
        layout.putConstraint(SpringLayout.WEST, left_button, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.WEST, mid_button, 0, SpringLayout.EAST, left_button);
        layout.putConstraint(SpringLayout.WEST, right_button, 0, SpringLayout.EAST, mid_button);
        layout.putConstraint(SpringLayout.WEST, label, 3, SpringLayout.EAST, left_button);
        layout.putConstraint(SpringLayout.NORTH, label, 1, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, right_button);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, mid_button);
    }
    
    public void setActive() {
        label.setForeground(Color.BLACK);
        
        ImageIcon icon, icon_tmp;
        icon = new ImageIcon(app_path + "images/tabs/left_tab_b.png");
        left_button.setIcon(icon);
        
        icon = new ImageIcon(app_path + "images/tabs/right_tab_b.png");
        right_button.setIcon(icon);
        
        icon_tmp = new ImageIcon(app_path + "images/tabs/mid_tab_b.png");
        icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(name_width + 4, icon_tmp.getIconHeight(), Image.SCALE_SMOOTH));
        mid_button.setIcon(icon);
        
        is_active = true;
    }
    
    public void setPassive() {
        label.setForeground(App.VERY_LIGHT_BLUE);
        
        ImageIcon icon, icon_tmp;
        icon = new ImageIcon(app_path + "images/tabs/left_tab_a.png");
        left_button.setIcon(icon);
        
        icon = new ImageIcon(app_path + "images/tabs/right_tab_a.png");
        right_button.setIcon(icon);
        
        icon_tmp = new ImageIcon(app_path + "images/tabs/mid_tab_a.png");
        icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(name_width + 4, icon_tmp.getIconHeight(), Image.SCALE_SMOOTH));
        mid_button.setIcon(icon);
        
        is_active = false;
    }
    
    /**
     * update the rider text to the current language
     */
    public void updateLanguage() {
        String name = SystemLink.getInstance().translate(getName());
        String variant;
        
        Font font = new Font("Comic Sans MS", Font.BOLD, 11);
        FontMetrics metrics = new FontMetrics(font) {};
        Rectangle2D bounds = metrics.getStringBounds(name, null);
        name_width = (int) bounds.getWidth();
        
        label.setText(name);
        
        if(is_active)
            variant = "b";
        else
            variant = "a";
        
        ImageIcon icon_tmp = new ImageIcon(app_path + "images/tabs/mid_tab_" + variant + ".png");
        ImageIcon icon = new ImageIcon(icon_tmp.getImage().getScaledInstance(name_width + 4, icon_tmp.getIconHeight(), Image.SCALE_SMOOTH));
        mid_button.setIcon(icon);
        mid_button.setPreferredSize(new Dimension(icon.getIconWidth(), icon.getIconHeight()));
        
        layout.putConstraint(SpringLayout.WEST, left_button, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.WEST, mid_button, 0, SpringLayout.EAST, left_button);
        layout.putConstraint(SpringLayout.WEST, right_button, 0, SpringLayout.EAST, mid_button);
        layout.putConstraint(SpringLayout.WEST, label, 3, SpringLayout.EAST, left_button);
        layout.putConstraint(SpringLayout.NORTH, label, 1, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.EAST, this, 0, SpringLayout.EAST, right_button);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, mid_button);
    }
}
